﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.UnitiveDocument.DocumentFlowPages
{
    /// <summary>
    /// ManageFlow 的摘要说明。
    /// </summary>
    public partial class ManageFlowPage : Page
    {
        private int _flowId;

        protected int FlowID
        {
            get { return _flowId; }
        }

        protected override void OnLoad(EventArgs e)
        {
            _flowId = Int32.Parse(Request.QueryString["FlowID"]);
            if (!Page.IsPostBack)
            {
                Bind();
            }

            base.OnLoad(e);
        }

        private void Bind()
        {
            labTitle.Text = Flow.GetFlowTitle(_flowId);

            dgStepList.DataSource = FlowStep.GetSteps(_flowId);
            dgStepList.DataBind();
        }

        protected static string GetFlowRuleName(FlowRule rule)
        {
            return Utility.GetEnumDisplayName(rule);
        }

#if false
        private string GetSelectedItemID(string controlID)
        {
            String selectedID;
            selectedID = "";
            //遍历DataGrid获得checked的ID
            foreach (DataGridItem item in dgStepList.Items)
            {
                if (((CheckBox) item.FindControl(controlID)).Checked)
                {
                    selectedID += dgStepList.DataKeys[item.ItemIndex] + ",";
                }
            }
            if (selectedID.Length > 0)
            {
                selectedID = selectedID.Substring(0, selectedID.Length - 1);
            }
            return selectedID;
        }
#endif

        public void MyDataGrid_Delete(object sender, DataGridCommandEventArgs e)
        {
            string StepID = dgStepList.DataKeys[e.Item.ItemIndex].ToString();
            FlowStep.DeleteStep(_flowId, Int32.Parse(StepID));
            Bind();
        }

        public void MyDataGrid_Move(object sender, DataGridCommandEventArgs e)
        {
            string StepID = dgStepList.DataKeys[e.Item.ItemIndex].ToString();
            
            FlowStep.MoveUpStep(_flowId, Int32.Parse(StepID));
            Bind();
        }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgStepList.CurrentPageIndex = e.NewPageIndex;
        }

        #endregion
    }
}